package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.ZhuCeSuccessBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class RegisterTwoActivity extends AppCompatActivity {

    private ImageView mZhucehuituiTwo;
    private Button mComplete;
    private Button mNoInput;
    private EditText et_activity_login_shehui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_register_two);
        initView();
        setOnClick();
    }
    private void setOnClick() {
        mZhucehuituiTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterTwoActivity.this.finish();
            }
        });
        mComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = getIntent().getExtras();
                String userName = bundle.getString("userName");
                String pwd = bundle.getString("pwd");
                String code = bundle.getString("code");
                String inviteCode = bundle.getString("inviteCode");
                String busLicenseNumber = et_activity_login_shehui.getText().toString();
                sendVerificationcode(userName,pwd,code,inviteCode,busLicenseNumber);
            }
        });
    }

    private void initView() {
        TextChange textChange = new TextChange();
        mZhucehuituiTwo = (ImageView) findViewById(R.id.mZhucehuituiTwo);
        mComplete = (Button) findViewById(R.id.mComplete);
        et_activity_login_shehui = (EditText) findViewById(R.id.et_activity_login_shehui);
        et_activity_login_shehui.addTextChangedListener(textChange);
        mNoInput = (Button) findViewById(R.id.mNoInput);
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
    //设置edittext的输入监听
    class TextChange implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            if (et_activity_login_shehui.length()>0){
                mNoInput.setVisibility(View.GONE);
                mComplete.setVisibility(View.VISIBLE);
            }
        }
    }
    //设置点击完成后的网络请求
    public void sendVerificationcode(String userName,String pwd,String code,String inviteCode,String busLicenseNumber) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userName",userName);
        params.put("pwd",pwd);
        params.put("code",code);
        params.put("inviteCode",inviteCode);
        params.put("busLicenseNumber",busLicenseNumber);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.registerTwoUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("zzz",""+result);
                String string = (String) result;
                Gson gson=new Gson();
                ZhuCeSuccessBean zhuCeSuccessBean = gson.fromJson(string, ZhuCeSuccessBean.class);
               // if (zhuCeSuccessBean.user.state.equals("1")){
                    ToastUtils.showShort(RegisterTwoActivity.this,""+zhuCeSuccessBean.result.message);
                    Intent intent=new Intent(RegisterTwoActivity.this,LoginActivity.class);
                    startActivity(intent);
                    RegisterTwoActivity.this.overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
              /*  }else {
                    ToastUtils.showShort(RegisterTwoActivity.this,""+zhuCeSuccessBean.result.message);
                }*/
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
}
